package com.changgou.goods.service.impl;

import com.changgou.goods.dao.SkuMapper;
import com.changgou.goods.service.SkuService;
import com.changgou.goods.pojo.Sku;
import com.changgou.order.pojo.OrderItem;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SkuServiceImpl implements SkuService {

    @Autowired
    private SkuMapper skuMapper;


    /**
     * @param searchMap
     * @return java.util.List<com.changgou.goods.pojo.Sku>
     * @description: 根据条件查询
     * @author 大佬味的小男孩
     * @date 2020/07/22 18:58
     */
    @Override
    public List<Sku> findList(HashMap<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return skuMapper.selectByExample(example);

    }

    /**
     * @param id
     * @return com.changgou.goods.pojo.Sku
     * @description: 根据id查询sku的商品数据
     * @author 大佬味的小男孩
     * @date 2020/08/01 11:02
     */
    @Override
    public Sku findById(String id) {
        return skuMapper.selectByPrimaryKey(id);
    }

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * @param username
     * @return void
     * @description: 1.获取购物车的数据 2.循环扣减库存并增加销量
     * @author 大佬味的小男孩
     * @date 2020/08/03 17:15
     */
    @Override
    @Transactional
    public void decrCount(String username) {

        // 1. 获取购物车的数据
        List<OrderItem> orderItemList = redisTemplate.boundHashOps("cart_" + username).values();
        //2. 循环扣减库存并增加销量
        for (OrderItem orderItem : orderItemList) {
            int count = skuMapper.decrCount(orderItem);
            if (count <= 0) {
                throw new RuntimeException("库存不足");
            }
        }


    }

    /**
     * @param skuId
     * @param num
     * @return void
     * @description: 回滚库存：增加库存并扣减销量
     * @author 大佬味的小男孩
     * @date 2020/08/06 10:20
     */
    @Override
    @Transactional
    public void Increasing(String skuId, Integer num) {
        int count = skuMapper.Increasing(skuId, num);
        if (count <= 0) {
            throw new RuntimeException("回滚订单失败");
        }
    }


    /**
     * @param searchMap
     * @return tk.mybatis.mapper.entity.Example
     * @description: 查询对象
     * @author 大佬味的小男孩
     * @date 2020/07/22 19:01
     */
    private Example createExample(HashMap<String, Object> searchMap) {
        Example example = new Example(Sku.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            // 商品id
            if (searchMap.get("id") != null && !"".equals(searchMap.get("id"))) {
                criteria.andEqualTo("id", searchMap.get("id"));
            }
            // 商品条码
            if (searchMap.get("sn") != null && !"".equals(searchMap.get("sn"))) {
                criteria.andEqualTo("sn", searchMap.get("sn"));
            }
            // SKU名称
            if (searchMap.get("name") != null && !"".equals(searchMap.get("name"))) {
                criteria.andLike("name", "%" + searchMap.get("name") + "%");
            }
            // 商品图片
            if (searchMap.get("image") != null && !"".equals(searchMap.get("image"))) {
                criteria.andLike("image", "%" + searchMap.get("image") + "%");
            }
            // 商品图片列表
            if (searchMap.get("images") != null && !"".equals(searchMap.get("images"))) {
                criteria.andLike("images", "%" + searchMap.get("images") + "%");
            }
            // SPUID
            if (searchMap.get("spuId") != null && !"".equals(searchMap.get("spuId"))) {
                criteria.andEqualTo("spuId", searchMap.get("spuId"));
            }
            // 类目名称
            if (searchMap.get("categoryName") != null && !"".equals(searchMap.get("categoryName"))) {
                criteria.andLike("categoryName", "%" + searchMap.get("categoryName") + "%");
            }
            // 品牌名称
            if (searchMap.get("brandName") != null && !"".equals(searchMap.get("brandName"))) {
                criteria.andLike("brandName", "%" + searchMap.get("brandName") + "%");
            }
            // 规格
            if (searchMap.get("spec") != null && !"".equals(searchMap.get("spec"))) {
                criteria.andLike("spec", "%" + searchMap.get("spec") + "%");
            }
            // 商品状态 1-正常，2-下架，3-删除
            if (searchMap.get("status") != null && !"".equals(searchMap.get("status"))) {
                criteria.andEqualTo("status", searchMap.get("status"));
            }
            // 价格（分）
            if (searchMap.get("price") != null) {
                criteria.andEqualTo("price", searchMap.get("price"));
            }
            // 库存数量
            if (searchMap.get("num") != null) {
                criteria.andEqualTo("num", searchMap.get("num"));
            }
            // 库存预警数量
            if (searchMap.get("alertNum") != null) {
                criteria.andEqualTo("alertNum", searchMap.get("alertNum"));
            }
            // 重量（克）
            if (searchMap.get("weight") != null) {
                criteria.andEqualTo("weight", searchMap.get("weight"));
            }
            // 类目ID
            if (searchMap.get("categoryId") != null) {
                criteria.andEqualTo("categoryId", searchMap.get("categoryId"));
            }
            // 销量
            if (searchMap.get("saleNum") != null) {
                criteria.andEqualTo("saleNum", searchMap.get("saleNum"));
            }
            // 评论数
            if (searchMap.get("commentNum") != null) {
                criteria.andEqualTo("commentNum", searchMap.get("commentNum"));
            }
        }
        return example;
    }
}
